package com.d3works.exception.service;

import com.d3works.rpc.ActionResponse;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HandleExceptionResponseImpl implements MethodInterceptor {

  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {

    if (invocation.getArguments() != null) {
      for (Object arg : invocation.getArguments()) {
        if (arg instanceof ActionResponse) {
          Object result = null;
          try {
            result = invocation.proceed();
          } catch (Exception e) {
            TraceBackService.trace((ActionResponse) arg, e);
          }
          return result;
        }
      }
      return invocation.proceed();
    } else {
      return invocation.proceed();
    }
  }
}
